package com.beetlestech.nfe.main.biz.com.impl;
import java.util.ArrayList;
import java.util.List;

import com.beetlestech.nfe.fw.core.biz.AbstractDBLogic;
import com.beetlestech.nfe.main.biz.com.HbOperationSaleCourse;
import com.beetlestech.nfe.main.biz.constant.CommonConstant;
import com.beetlestech.nfe.main.biz.constant.CtgCodeConstant;
import com.beetlestech.nfe.main.biz.constant.HbMessageCode;
import com.beetlestech.nfe.main.biz.constant.CtgCodeConstant.CATAGORY_P_MESSAGE_STATE;
import com.beetlestech.nfe.main.db.constant.SQLConstant;
import com.beetlestech.nfe.main.db.tbl.MstcourseDTO;
import com.beetlestech.nfe.main.db.tbl.OrderdetailDTO;
import com.beetlestech.nfe.main.db.tbl.StudentorderDTO;
import com.beetlestech.nfe.main.db.tbl.StudyhistoryDTO;
import com.beetlestech.nfe.main.db.tbl.UsermessageDTO;
import com.beetlestech.nfe.main.db.view.VcourseidandlessonidforopenDTO;

public class HbOperationSaleCourseImpl extends AbstractDBLogic implements HbOperationSaleCourse, CtgCodeConstant, HbMessageCode, CommonConstant, SQLConstant {
	@Override
	public void operationSaleCourse(
			String actionType,
			String singleOrMany,
			Integer orderId,
			Integer goodsId,
			String idSql
			) {
		if(actionType.equals("thaw")){
			OrderdetailDTO orderdetailDTO=new OrderdetailDTO();
			if(singleOrMany.equals("single")){
				orderdetailDTO.setorderid(orderId);
				orderdetailDTO.setgoodsid(goodsId);
				orderdetailDTO.setstate(Integer.parseInt(LEARNING_STATE.LEARNING.toString()));
				updateDAO.updateOne(OrderdetailDTO.UPDATE, orderdetailDTO, M10190WS);
				
				//查询学生id
				StudentorderDTO  studentDTO=new StudentorderDTO();
				studentDTO.setorderid(orderId);
				StudentorderDTO sDTO = queryDAO.queryForObject(StudentorderDTO.EQUAL_SEARCH, studentDTO);
				//查询课程名称
				MstcourseDTO  mc=new MstcourseDTO();
				mc.setcourseid(goodsId);
				mc=queryDAO.queryForObject(MstcourseDTO.EQUAL_SEARCH, mc);
				createMessageJd(mc.getname(),sDTO.getstudentid());//解冻后给学生发送一条消息
			}else if(singleOrMany.equals("many")){
				String[] dbSqlArray=idSql.split("-");
				for(int i=0;i<dbSqlArray.length;i++){
					String[] SpitOrderAndGoodsArray=dbSqlArray[i].split(",");
					orderdetailDTO.setorderid(Integer.parseInt(SpitOrderAndGoodsArray[0]));
					orderdetailDTO.setgoodsid(Integer.parseInt(SpitOrderAndGoodsArray[1]));
					orderdetailDTO.setstate(Integer.parseInt(LEARNING_STATE.LEARNING.toString()));
					updateDAO.updateOne(OrderdetailDTO.UPDATE, orderdetailDTO, M10190WS);

					//查询学生id
					StudentorderDTO  studentDTO=new StudentorderDTO();
					studentDTO.setorderid(Integer.parseInt(SpitOrderAndGoodsArray[0]));
					StudentorderDTO sDTO = queryDAO.queryForObject(StudentorderDTO.EQUAL_SEARCH, studentDTO);
					//查询课程名称
					MstcourseDTO  mc=new MstcourseDTO();
					mc.setcourseid(Integer.parseInt(SpitOrderAndGoodsArray[1]));
					mc=queryDAO.queryForObject(MstcourseDTO.EQUAL_SEARCH, mc);
					createMessageJd(mc.getname(),sDTO.getstudentid());//解冻后给学生发送一条消息

				}
			}
			
		}else if(actionType.equals("frozen")){
			OrderdetailDTO orderdetailDTO=new OrderdetailDTO();
			if(singleOrMany.equals("single")){
				orderdetailDTO.setorderid(orderId);
				orderdetailDTO.setgoodsid(goodsId);
				orderdetailDTO.setstate(Integer.parseInt(LEARNING_STATE.FROZEN.toString()));
				updateDAO.updateOne(OrderdetailDTO.UPDATE, orderdetailDTO, M10190WS);
				
				//查询学生id
				StudentorderDTO  studentDTO=new StudentorderDTO();
				studentDTO.setorderid(orderId);
				StudentorderDTO sDTO = queryDAO.queryForObject(StudentorderDTO.EQUAL_SEARCH, studentDTO);
				//查询课程名称
				MstcourseDTO  mc=new MstcourseDTO();
				mc.setcourseid(goodsId);
				mc=queryDAO.queryForObject(MstcourseDTO.EQUAL_SEARCH, mc);
				createMessageDj(mc.getname(),sDTO.getstudentid());//冻结后给学生发送一条消息
			}else if(singleOrMany.equals("many")){
				String[] dbSqlArray=idSql.split("-");
				for(int i=0;i<dbSqlArray.length;i++){
					String[] SpitOrderAndGoodsArray=dbSqlArray[i].split(",");
					orderdetailDTO.setorderid(Integer.parseInt(SpitOrderAndGoodsArray[0]));
					orderdetailDTO.setgoodsid(Integer.parseInt(SpitOrderAndGoodsArray[1]));
					orderdetailDTO.setstate(Integer.parseInt(LEARNING_STATE.FROZEN.toString()));
					updateDAO.updateOne(OrderdetailDTO.UPDATE, orderdetailDTO, M10190WS);
					
					//查询学生id
					StudentorderDTO  studentDTO=new StudentorderDTO();
					studentDTO.setorderid(Integer.parseInt(SpitOrderAndGoodsArray[0]));
					StudentorderDTO sDTO = queryDAO.queryForObject(StudentorderDTO.EQUAL_SEARCH, studentDTO);
					//查询课程名称
					MstcourseDTO  mc=new MstcourseDTO();
					mc.setcourseid(Integer.parseInt(SpitOrderAndGoodsArray[1]));
					mc=queryDAO.queryForObject(MstcourseDTO.EQUAL_SEARCH, mc);
					createMessageDj(mc.getname(),sDTO.getstudentid());//冻结后给学生发送一条消息
				}
			}
			
		}else if(actionType.equals("open")){
			OrderdetailDTO orderdetailDTO=new OrderdetailDTO();
			VcourseidandlessonidforopenDTO vcourseidandlessonidforopenDTO =new VcourseidandlessonidforopenDTO();
			List<VcourseidandlessonidforopenDTO> courseidandlessonidArray=new ArrayList<VcourseidandlessonidforopenDTO>();
			if(singleOrMany.equals("single")){
				orderdetailDTO.setorderid(orderId);
				orderdetailDTO.setgoodsid(goodsId);
				vcourseidandlessonidforopenDTO.setcourseid(goodsId);
				courseidandlessonidArray=queryDAO.queryForObjectList(VcourseidandlessonidforopenDTO.EQUAL_SEARCH, vcourseidandlessonidforopenDTO);
				int i=0;
				/**********************Bug fix 20151009 qindong******************************
				for (VcourseidandlessonidforopenDTO item : courseidandlessonidArray) {
					StudyhistoryDTO studyhistoryDTO=new StudyhistoryDTO();
					studyhistoryDTO.setorderid(orderId);
					studyhistoryDTO.setcourseid(goodsId);
					studyhistoryDTO.setlessonid(item.getlessonid());
					if(i==0){
						studyhistoryDTO.setstate(1);
					}else{
						studyhistoryDTO.setstate(0);
					}
					studyhistoryDTO.setisfirstwatch(1);
					studyhistoryDTO.setlastdate(getCurrentDate());
					studyhistoryDTO.setlasttime(getCurrentTime());
					updateDAO.updateOne(StudyhistoryDTO.INSERT,studyhistoryDTO,M10040WS);
					
					i++;
				}
				*/
				orderdetailDTO.setstate(Integer.parseInt(LEARNING_STATE.LEARNING.toString()));
				updateDAO.updateOne(OrderdetailDTO.UPDATE, orderdetailDTO, M10190WS);
				//查询学生id
				StudentorderDTO  studentDTO=new StudentorderDTO();
				studentDTO.setorderid(orderId);
				StudentorderDTO sDTO = queryDAO.queryForObject(StudentorderDTO.EQUAL_SEARCH, studentDTO);
				//查询课程名称
				MstcourseDTO  mc=new MstcourseDTO();
				mc.setcourseid(goodsId);
				mc=queryDAO.queryForObject(MstcourseDTO.EQUAL_SEARCH, mc);
				createMessage(mc.getname(),sDTO.getstudentid());//开通后给学生发送一条消息
				
			}else if(singleOrMany.equals("many")){
				String[] dbSqlArray=idSql.split("-");
				for(int i=0;i<dbSqlArray.length;i++){
					String[] SpitOrderAndGoodsArray=dbSqlArray[i].split(",");
					orderdetailDTO.setorderid(Integer.parseInt(SpitOrderAndGoodsArray[0]));
					orderdetailDTO.setgoodsid(Integer.parseInt(SpitOrderAndGoodsArray[1]));
					vcourseidandlessonidforopenDTO.setcourseid(Integer.parseInt(SpitOrderAndGoodsArray[1]));
					courseidandlessonidArray=queryDAO.queryForObjectList(VcourseidandlessonidforopenDTO.EQUAL_SEARCH, vcourseidandlessonidforopenDTO);
					int j=0;
					/**********************Bug fix 20151009 qindong******************************
					for (VcourseidandlessonidforopenDTO item : courseidandlessonidArray) {
						StudyhistoryDTO studyhistoryDTO=new StudyhistoryDTO();
						studyhistoryDTO.setorderid(Integer.parseInt(SpitOrderAndGoodsArray[0]));
						studyhistoryDTO.setcourseid(Integer.parseInt(SpitOrderAndGoodsArray[1]));
						studyhistoryDTO.setlessonid(item.getlessonid());
						if(j==0){
							studyhistoryDTO.setstate(1);
						}else{
							studyhistoryDTO.setstate(0);
						}
						studyhistoryDTO.setisfirstwatch(1);
						studyhistoryDTO.setlastdate(getCurrentDate());
						studyhistoryDTO.setlasttime(getCurrentTime());
						updateDAO.updateOne(StudyhistoryDTO.INSERT,studyhistoryDTO,M10040WS);
						j++;
					}
					*/
					orderdetailDTO.setstate(Integer.parseInt(LEARNING_STATE.LEARNING.toString()));
					updateDAO.updateOne(OrderdetailDTO.UPDATE, orderdetailDTO, M10190WS);
					
					//查询学生id
					StudentorderDTO  studentDTO=new StudentorderDTO();
					studentDTO.setorderid(Integer.parseInt(SpitOrderAndGoodsArray[0]));
					StudentorderDTO sDTO = queryDAO.queryForObject(StudentorderDTO.EQUAL_SEARCH, studentDTO);
					//查询课程名称
					MstcourseDTO  mc=new MstcourseDTO();
					mc.setcourseid(Integer.parseInt(SpitOrderAndGoodsArray[1]));
					mc=queryDAO.queryForObject(MstcourseDTO.EQUAL_SEARCH, mc);
					createMessage(mc.getname(),sDTO.getstudentid());//开通后给学生发送一条消息
				}
			}
			
		}

	}
	//开通：您购买的【课程名】已正式开通，可以开始学习。
	private void createMessage(String courseName, Integer studentID){
		// 创建消息
		UsermessageDTO userMessage = new UsermessageDTO();
		userMessage.setTitel("您购买的【" + courseName + "】已正式开通，可以开始学习!");
		userMessage.setMessage("您购买的【" + courseName + "】已正式开通，可以开始学习!");
		userMessage.setSenderId(SERVER_ID);
		userMessage.setReceiveId(studentID);
		userMessage.setSendDT(getCurrentDate());
		userMessage.setSendTime(getCurrentTime());
		userMessage.setReadflg(Integer.parseInt(CATAGORY_P_MESSAGE_STATE.UNREAD.toString()));
		updateDAO.update(UsermessageDTO.INSERT, userMessage);
	}
	//您的课程【课程名】已被冻结，如需继续学习，请申请解冻
	private void createMessageDj(String courseName, Integer studentID){
		// 创建消息
		UsermessageDTO userMessage = new UsermessageDTO();
		userMessage.setTitel("您的课程【" + courseName + "】已被冻结，如需继续学习，请申请解冻!");
		userMessage.setMessage("您的课程【" + courseName + "】已被冻结，如需继续学习，请申请解冻!");
		userMessage.setSenderId(SERVER_ID);
		userMessage.setReceiveId(studentID);
		userMessage.setSendDT(getCurrentDate());
		userMessage.setSendTime(getCurrentTime());
		userMessage.setReadflg(Integer.parseInt(CATAGORY_P_MESSAGE_STATE.UNREAD.toString()));
		updateDAO.update(UsermessageDTO.INSERT, userMessage);
	}
	//您被冻结的课程【课程名】已经解冻，可以继续学习
	private void createMessageJd(String courseName, Integer studentID){
		// 创建消息
		UsermessageDTO userMessage = new UsermessageDTO();
		userMessage.setTitel("您被冻结的课程【" + courseName + "】已经解冻，可以继续学习!");
		userMessage.setMessage("您被冻结的课程【" + courseName + "】已经解冻，可以继续学习!");
		userMessage.setSenderId(SERVER_ID);
		userMessage.setReceiveId(studentID);
		userMessage.setSendDT(getCurrentDate());
		userMessage.setSendTime(getCurrentTime());
		userMessage.setReadflg(Integer.parseInt(CATAGORY_P_MESSAGE_STATE.UNREAD.toString()));
		updateDAO.update(UsermessageDTO.INSERT, userMessage);
	}
	
}
